package light.head.controller.inv;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.inv.InvBusinessTypeDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Organ;
import light.head.model.inv.InvBusinessType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = Url.C_INVBUSINESSTYPE)
public class InvBusinessTypeController extends SupporterController{
	
	private InvBusinessTypeDAO ibtDAO;
	private OrganDAO organDAO;
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.INVBUSINESSTYPE_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.ADDRESS, Parm.DELETED, Parm.OC, Parm.E_MAIL);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (ibtDAO.findByMap(organDAO.getSonCodeList(oc), map, pb) == RESULT.SUCCESS)
			request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.INVBUSINESSTYPE_LIST);
	}

	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			InvBusinessType ibt = ibtDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.INVBUSINESSTYPE, ibt);
		}
		return new ModelAndView(Url.INVBUSINESSTYPE_EDIT);
	}
	
	
	@RequestMapping(value = Url.SAVE)
	public void save(InvBusinessType ibt, HttpServletRequest request, HttpServletResponse response) throws Exception{
		if (ibt == null) {
			print(response, Msg.PARAMETER_ERROR);
			return ;
		}
		RESULT result = RESULT.ERROR;
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		List<InvBusinessType> list = ibtDAO.findByNameOc(ibt.getName(), oc);
		if (list != null && list.size() > 0) {
			print(response, Msg.EXISTS);
			return ;
		}
		if (ibt.getCode() == null) {
			ibt.setCu(cu_code(request));
			ibt.setCt(new Date());
			ibt.setOc(oc);
			result = ibtDAO.add(ibt);
		} else {
			result = ibtDAO.upd(ibt);
		} if (RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		} else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		InvBusinessType ibt = ibtDAO.get(Integer.parseInt(code));
		if (null == ibt) {
			print(response, Msg.NOT_FIND);
			return;
		}
		ibt.setDeleted(true);
		if (ibtDAO.upd(ibt) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		InvBusinessType ibt = ibtDAO.get(Integer.parseInt(code));
		if (null == ibt) {
			print(response, Msg.NOT_FIND);
			return;
		}
		ibt.setDeleted(false);
		if (ibtDAO.upd(ibt) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	
	@RequestMapping(Url.AID_INIT)
	public ModelAndView aidInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer code = cu_organCode(request);
 		if (null != code) {
 			List<Organ> list = organDAO.getAll();
			Organ o = findOgranByCode(code, list);
			if (null != o) {
				StringBuffer tree = new StringBuffer();
				if (!o.getDeleted()) {
					tree.append("p.add('" + o.getCode() + "','-1','"+ o.getName() + "');");
					aidTree(tree, o.getSonList());
				}
			
			request.setAttribute(Parm.TREE, tree);
			request.setAttribute(Parm.ORGAN, list);
			}
 		}
 		request.setAttribute(Parm.ID, request.getParameter(Parm.ID));
		request.setAttribute(Parm.NAME, request.getParameter(Parm.NAME));
		return new ModelAndView(Url.INVBUSINESSTYPE_AID_INIT);
	}
	
	private Organ findOgranByCode(Integer code, List<Organ> list){
		if(null != code && null != list && 0 < list.size()){
			for (Organ o : list){
				if(code.intValue() == o.getCode().intValue()){
					return o;
				} else {
					Organ or = findOgranByCode(code, o.getSonList());
					if (or != null) {
						return or;
					}
				}
			}
		}
		return null;
	}
	
	private void aidTree(StringBuffer tree, List<Organ> list) {
		if (null == tree || null == list || list.size() == 0)
			return;
		for (Organ o : list) {
			tree.append("p.add('" + o.getCode() + "','" + o.getPc() + "','" + o.getName() + "');");
			aidTree(tree, o.getSonList());
		}
	}

	
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
	@Autowired
	public void setIbtDAO(InvBusinessTypeDAO ibtDAO) {
		this.ibtDAO = ibtDAO;
	}
	
}
